package com.zhuang.stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;

public class CreateSteam {

	public static void main(String[] args) {
		CreateSteam createSteam = new CreateSteam();

		createSteam.getStream01();
		createSteam.getStream02();
		createSteam.getStream03();
		createSteam.getStream04();
		createSteam.getStream05();
		createSteam.getStream06();
		createSteam.getStream07();
		createSteam.getStream08();
		createSteam.getStream09();
	}

	/**
	 * 
	 * @Title: getStream01
	 * @Description: Using Collection
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream01() {
		ArrayList<Object> list = new ArrayList<>();
		list.add("Geeks");
		list.add("for");
		list.add("Geeks");
		Stream<Object> stream = list.stream();
		Iterator<Object> iterator = stream.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next() + " ");
		}
	}

	/**
	 * 
	 * @Title: getStream02
	 * @Description: Stream.of(T…t) 方法可用于创建具有指定 T 值的流，其中 T 是元素。这个方法返回一个包含 t 个元素的顺序流
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream02() {
		Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
		stream.forEach(s -> System.out.println(s + " "));
	}

	/**
	 * 
	 * @Title: getStream03
	 * @Description: Create stream using Arrays.stream()
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream03() {
		String[] arr = new String[] { "a", "b", "c" };
		Stream<String> stream = Arrays.stream(arr);
		Iterator<String> iterator = stream.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next() + " ");
		}
	}

	/**
	 * 
	 * @Title: getStream04
	 * @Description: Create stream using Stream.of() A non interfering action to be
	 *               perform on elements as they are consumed from the stream and
	 *               returns also a new stream.
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream04() {
		String[] arr = new String[] { "a", "b", "c" };
		Stream<String> stream = Stream.of(arr);
		Iterator<String> iterator = stream.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next() + " ");
		}
	}

	/**
	 * 
	 * @Title: getStream05
	 * @Description: Create an empty stream using Stream.empty()
	 * @author: KangXiaoZhuang
	 * @param: 空()方法在创建时使用，以避免为没有元素的流返回 null
	 * @return: void
	 * @throws
	 */
	public void getStream05() {
		Stream<Object> stream = Stream.empty();
		Iterator<Object> iterator = stream.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next() + " ");
		}
	}

	/**
	 * 
	 * @Title: getStream06
	 * @Description: 当应该在语句的右边部分另外指定所需的类型时，使用 builder()方法，否则 build()方法将创建流的实例
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream06() {
		Builder<Object> builder = Stream.builder();
		Stream<Object> stream = builder.add("a").add("b").add("c").build();
		Iterator<Object> iterator = stream.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next() + " ");
		}
	}

	/**
	 * 
	 * @Title: getStream07
	 * @Description: Create an infinite Stream using Stream.iterate()
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream07() {
		int seedValue = 2;
		int limitTerms = 5;
		Stream.iterate(seedValue, (Integer n) -> n * n).limit(limitTerms).forEach(System.out::println);
	}

	/**
	 * 
	 * @Title: getStream08
	 * @Description:Create an infinite Stream using Stream.generate() method
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream08() {
		int limitTerms = 5;
		Stream.generate(Math::random).limit(limitTerms).forEach(System.out::println);
	}

	/**
	 * 
	 * @Title: getStream09
	 * @Description: Create stream from a Pattern using Predicate 在 java 8 中，Pattern
	 *               的谓词 asPredicate()方法创建了一个用于模式匹配的谓词布尔值函数
	 * @author: KangXiaoZhuang
	 * @param:
	 * @return: void
	 * @throws
	 */
	public void getStream09() {
		Pattern pattern = Pattern.compile("^G");
		List<String> list = Arrays.asList("Geeks", "For", "Geek", "GeeksForGeeks", "A Computer Portal");
		list.stream().filter(pattern.asPredicate()).forEach(System.out::println);
	}
}
